home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / mit / avltest / avltest.c next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  3.5 KB  |  184 lines

  1.  
  2. /*
  3.  *    $Header: avltest.c,v 3.0 91/05/17 16:15:16 jrd Rel $
  4.  *    Author: J. Davin
  5.  *    Copyright 1988, 1989, Massachusetts Institute of Technology
  6.  *    See permission and disclaimer notice in file "notice.h"
  7.  */
  8.  
  9. #include    <notice.h>
  10.  
  11. #include    <stdio.h>
  12.  
  13. #include    <ctypes.h>
  14. #include    <rdx.h>
  15. #include    <avl.h>
  16. #include    <local.h>
  17.  
  18. int        rand ();
  19. int        srand ();
  20.  
  21. AvlBalanceType        nodeCmp (a, b, n)
  22.  
  23. AvlInfoType        a;
  24. AvlNamePtrType        b;
  25. AvlLengthType        n;
  26.  
  27. {
  28.     int        cmp;
  29.     AvlBalanceType    result;
  30.  
  31.     n = n;
  32.     cmp = strcmp ((char *) a, (char *) b);
  33.     if (cmp > 0) {
  34.         result = avlDirLeft;
  35.     }
  36.     else if (cmp < 0) {
  37.         result = avlDirRight;
  38.     }
  39.     else {
  40.         result = avlDirBalanced;
  41.     }
  42.     return (result);
  43. }
  44.  
  45. CBytePtrType        randString (n)
  46.  
  47. CIntfType        n;
  48.  
  49. {
  50.     CUnsfType        len;
  51.     CBytePtrType        item;
  52.     CBytePtrType        cp;
  53.  
  54.     while ((len = rand () % n) == 0);
  55.     item = (CBytePtrType) malloc ((unsigned) (len + 1));
  56.     if (item != (CBytePtrType) 0) {
  57.         cp = item;
  58.         while (len-- != 0) {
  59.             *cp++ = (CByteType) ((rand () % 26) + 'A');
  60.         }
  61.         *cp = (CByteType) 0;
  62.     }
  63.  
  64.     return (item);
  65. }
  66.  
  67. AvlStatusType        stringPrint (x)
  68.  
  69. AvlInfoType        x;
  70.  
  71. {
  72.     printf ("%s", (char *) x);
  73.     return (errOk);
  74. }
  75.  
  76. #define        TESTSIZE    100
  77.  
  78. int    main (argc, argv)
  79.  
  80. int        argc;
  81. char        *argv [];
  82.  
  83. {
  84.     AvlIdType        tree;
  85.     CBytePtrType        info;
  86.     AvlInfoType        find;
  87.     CIntfType        i;
  88.     CIntfType        filter;
  89.     CIntfType        testsize;
  90.     CIntfType        howmany;
  91.     AvlStatusType        status;
  92.     CBytePtrType        inputs [ TESTSIZE + 1 ];
  93.     CUnslType        number;
  94.  
  95.     if (argc < 2) {
  96.         printf ("usage: %s [ testsize ] < seed > < filter >\n",
  97.             argv [ 0 ]);
  98.         exit (1);
  99.     }
  100.  
  101.     if (rdxDecodeAny (& number, argv [ 1 ]) < (CIntfType) 0) {
  102.         printf ("%s: bad testsize %s\n", argv [ 0 ], argv [ 1 ]);
  103.         exit (2);
  104.     }
  105.     testsize = (CIntfType) number;
  106.     if (testsize > TESTSIZE) {
  107.         printf ("%s: testsize < %d\n", argv [ 0 ], TESTSIZE);
  108.         exit (2);
  109.     }
  110.  
  111.     if (argc > 2) {
  112.         if (rdxDecodeAny (& number, argv [ 2 ]) < (CIntfType) 0) {
  113.             printf ("%s: bad seed %s\n", argv [ 0 ],
  114.                 argv [ 2 ]);
  115.             exit (2);
  116.         }
  117.         (void) srand ((int) number);
  118.     }
  119.  
  120.     if (argc > 3) {
  121.         if (rdxDecodeAny (& number, argv [ 3 ]) < (CIntfType) 0) {
  122.             printf ("%s: bad filter %s\n", argv [ 0 ],
  123.                 argv [ 3 ]);
  124.             exit (2);
  125.         }
  126.         filter = (CIntfType) number;
  127.     }
  128.     else {
  129.         filter = (CIntfType) 0;
  130.     }
  131.  
  132.     tree = avlNew (nodeCmp, stringPrint);
  133.     status = errOk;
  134.  
  135.     for (i = testsize; (i != 0) && (status == errOk); i--) {
  136.         info = randString (29);
  137.         inputs [ i ] = info;
  138.         status = avlInsert (tree, (AvlNamePtrType) info,
  139.             (AvlLengthType) strlen ((char *) info),
  140.             (AvlInfoType) info);
  141.         printf ("avlInsert: tree %08.08X info %s\n", tree, info);
  142.     }
  143.  
  144.     printf ("status: %d\n", status);
  145.     printf ("\n");
  146.     (void) fflush (stdout);
  147.  
  148.     howmany = testsize - ((i != 0) ? i + 1 : 0);
  149.  
  150.     for (i = howmany; (i != 0); i--) {
  151.         info = inputs [ i ];
  152.         find = avlFind (tree, (AvlNamePtrType) info,
  153.             (AvlLengthType) strlen ((char *) info));
  154.         if (strcmp ((char *) find, (char *) info) != 0) {
  155.             printf ("avlFind: find %s info %s\n", find, info);
  156.         }
  157.     }
  158.  
  159.     info = (CBytePtrType) "";
  160.  
  161.     while ((find = avlCessor (tree, (AvlNamePtrType) info,
  162.         (AvlLengthType) strlen ((char *) info))) !=
  163.         (AvlInfoType) 0) {
  164.         printf ("avlCessor: %s\n", (char *) find);
  165.         info = (CBytePtrType) find;
  166.     }
  167.     
  168.     status = errOk;
  169.  
  170.     for (i = testsize; (i != 0) && (status == errOk); i--) {
  171.         if ((rand () & filter) == 0) {
  172.             info = inputs [ i ];
  173.             status = avlRemove (tree, (AvlNamePtrType) info,
  174.                 (AvlLengthType) strlen ((char *) info));
  175.             printf ("avlRemove: tree %08.08X info %s\n",
  176.                 tree, info);
  177.         }
  178.     }
  179.  
  180.     printf ("status: %d\n", status);
  181.  
  182. }
  183.  
  184.